package com.xjj.divideandconquer;

public class M0050PowxN {

    public double myPow(double x, int n) {
        // 注意类型问题,int long频繁转换会超时的
        long num = n;
        return n > 0 ? calc(x, n) : 1.0 / calc(x, -num);
    }

    private double calc(double x, long n) {
        if (n == 0) {
            return 1;
        }
        // 算两遍会超时
        double result = calc(x, n / 2);
        if (n % 2 == 0) {
            return result * result;
        } else {
            return result * result * x;
        }
    }

}
